package cn.rengy.web.framework.wxmp.message;

import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.rengy.web.framework.cache.CacheService;
import cn.rengy.web.framework.constants.Constants;
import cn.rengy.web.framework.id.IdGenerator;
import cn.rengy.web.framework.sql.DbService;
@Service
public class WxMsgTemplateDataService {
	@Autowired
	private DbService dbService;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private IdGenerator idGenerator;
	public void add(Map<String,Object> param) {
		String template_instance_id=MapUtils.getString(param, "template_instance_id");
		String id=idGenerator.nextId().toString();
		param.put("id", id);
		this.dbService.updateMapParameter("insert_wx_msg_template_data", param);
		//删除缓存
		cacheService.evict(Constants.KEY_TemplateInstance, template_instance_id);
	}
	
	public void update(Map<String,Object> param) {
		String template_instance_id=MapUtils.getString(param, "template_instance_id");
		this.dbService.updateMapParameter("update_wx_msg_template_data", param);
		//删除缓存
		cacheService.evict(Constants.KEY_TemplateInstance, template_instance_id);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void delete(Map<String,Object> param) {
		String id=MapUtils.getString(param, "id");
		String template_instance_id=MapUtils.getString(param, "template_instance_id");
		//删除明细
		this.dbService.update("delete_wx_msg_template_data", id);
		//删除缓存
		cacheService.evict(Constants.KEY_TemplateInstance, template_instance_id);
	}
}
